package com.hjq.demo.yuliao.activity

import android.content.Context
import android.content.Intent
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.dylanc.mmkv.MMKVOwner
import com.gyf.immersionbar.ImmersionBar
import com.hjq.base.BaseDialog
import com.hjq.demo.R
import com.hjq.demo.app.vb.AppVbActivity
import com.hjq.demo.databinding.ActivityBackPackBinding
import com.hjq.demo.databinding.ActivitySettingsMyBinding
import com.hjq.demo.databinding.ActivityWithdrawBinding
import com.hjq.demo.http.glide.GlideApp
import com.hjq.demo.manager.CacheDataManager
import com.hjq.demo.ui.dialog.MessageDialog
import com.hjq.demo.utils.CommonSpaceItemDecoration
import com.hjq.demo.yuliao.adapter.BackPackAdapter
import com.hjq.demo.yuliao.dialog.LogoutDialog
import com.hjq.permissions.XXPermissions
import io.rong.imkit.RongIM
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 *
 * @author jiahe
 * 创建日期 2022/2/9
 * 描述 设置
 *
 */
class SettingsMyActivity : AppVbActivity<ActivitySettingsMyBinding>(),MMKVOwner {

    companion object {

        private const val INTENT_KEY_INFO: String = "info"

        fun start(context: Context, info: String) {
            val intent = Intent(context, SettingsMyActivity::class.java)
            intent.putExtra(INTENT_KEY_INFO, info)
            context.startActivity(intent)
        }
    }

    override fun initView() {
        binding.sbClearCache.setRightText(CacheDataManager.getTotalCacheSize(this))

        setOnClickListener(
            binding.sbClearCache,
            binding.sbLogout,
            binding.sbChangePsw,
            binding.sbAboutUs,
            binding.sbFeedback
        )
    }

    override fun initData() {

    }

    override fun onClick(view: View) {
        super.onClick(view)
        if (view == binding.sbClearCache) {
            // 清除内存缓存（必须在主线程）
            GlideApp.get(this).clearMemory()
            lifecycleScope.launch(Dispatchers.IO) {
                CacheDataManager.clearAllCache(this@SettingsMyActivity)
                // 清除本地缓存（必须在子线程）
                GlideApp.get(this@SettingsMyActivity).clearDiskCache()
                withContext(Dispatchers.Main) {
                    // 重新获取应用缓存大小
                    binding.sbClearCache.setRightText(CacheDataManager.getTotalCacheSize(this@SettingsMyActivity))
                }
            }
        } else if (view == binding.sbLogout) {
            LogoutDialog.Builder(this)
                .setTitle(null)
                .setMessage("确定退出当前账号？ ")
                .setConfirm(R.string.common_confirm)
                .setCancel(R.string.common_cancel)
                .setCancelable(false)
                .setListener(object : LogoutDialog.OnListener {

                    override fun onConfirm(dialog: BaseDialog?) {
                        toast("退出登录")
                        RongIM.getInstance().logout()
                        kv.clearAll()
                        LoginMyActivity.start(this@SettingsMyActivity,"")
                    }
                })
                .show()
        } else if (view == binding.sbChangePsw) {
            ChangePswActivity.start(this, "", "", object : ChangePswActivity.OnRegisterListener {
                override fun onSucceed(phone: String?, password: String?) {

                }

            })
        } else if (view == binding.sbAboutUs) {
            AboutUsActivity.start(this, "")
        } else if (view == binding.sbFeedback) {
            FeedBackActivity.start(this, "")
        }
    }
}